package com.cheer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.OaLeamPeriodsDao;
import com.cheer.dto.OaLearnPeriodAddDTO;
import com.cheer.dto.OaLearnPeriodUpdateDTO;
import com.cheer.entity.OaLeamPeriodsEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.OaLeamPeriodsService;
import com.cheer.vo.OaLearnPeriodInfoVO;
import com.cheer.vo.OaLearnPeriodListVO;
import com.cheer.vo.SimpleObjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("oaLeamPeriodsService")
public class OaLeamPeriodsServiceImpl extends ServiceImpl<OaLeamPeriodsDao, OaLeamPeriodsEntity> implements OaLeamPeriodsService {

    @Autowired
    private OaLeamPeriodsDao oaLeamPeriodsDao;

    @Override
    public void add(OaLearnPeriodAddDTO learnPeriodAddDTO) {
        OaLeamPeriodsEntity leamPeriods = new OaLeamPeriodsEntity()
                .setName(learnPeriodAddDTO.getName())
                .setDescribe(learnPeriodAddDTO.getDescribe())
                .setAddTime(String.valueOf(DateUtil.currentSeconds()));

        oaLeamPeriodsDao.insert(leamPeriods);
    }

    @Override
    public Page<OaLearnPeriodListVO> list(Integer pageNum, Integer pageSize) {
        IPage<OaLeamPeriodsEntity> page = oaLeamPeriodsDao.selectPage(pageNum, pageSize);

        List<OaLearnPeriodListVO> list = page.getRecords().stream()
                .map(learnPeriod -> new OaLearnPeriodListVO()
                        .setId(learnPeriod.getId())
                        .setName(learnPeriod.getName())
                        .setDescribe(learnPeriod.getDescribe()))
                .collect(Collectors.toList());

        return new Page<>(page, list);
    }

    @Override
    public OaLearnPeriodInfoVO info(Integer id) {
        OaLeamPeriodsEntity learnPeriod = oaLeamPeriodsDao.selectById(id);
        if (learnPeriod == null) {
            throw new ServiceException("学习不存在");
        }

        return new OaLearnPeriodInfoVO()
                .setId(learnPeriod.getId())
                .setName(learnPeriod.getName())
                .setDescribe(learnPeriod.getDescribe());
    }

    @Override
    public void update(OaLearnPeriodUpdateDTO learnPeriodUpdateDTO) {
        OaLeamPeriodsEntity learnPeriod = oaLeamPeriodsDao.selectById(learnPeriodUpdateDTO.getId());
        if (learnPeriod == null) {
            throw new ServiceException("学习不存在");
        }

        OaLeamPeriodsEntity updateLearnPeriod = new OaLeamPeriodsEntity()
                .setId(learnPeriodUpdateDTO.getId())
                .setName(learnPeriodUpdateDTO.getName())
                .setDescribe(learnPeriodUpdateDTO.getDescribe());

        oaLeamPeriodsDao.updateById(updateLearnPeriod);
    }

    @Override
    public List<SimpleObjectVO> period() {
        List<OaLeamPeriodsEntity> list = oaLeamPeriodsDao.selectList();

        return list.stream()
                .map(learnPeriod -> new SimpleObjectVO()
                        .setId((long) learnPeriod.getId())
                        .setName(learnPeriod.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public List<OaLeamPeriodsEntity> listByOrder() {
        return oaLeamPeriodsDao.selectList(new QueryWrapper<OaLeamPeriodsEntity>()
            .orderByDesc("id"));
    }
}